﻿using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using Shell.PocoDot.Configurations;

namespace Shell.PocoDot
{
    static class DbModelCompiler
    {
        private static Dictionary<ProviderConfiguration, DbCompiledModel> models;

        static DbModelCompiler()
        {
            models = new Dictionary<ProviderConfiguration, DbCompiledModel>();
        }

        public static DbCompiledModel Compile(this ProviderConfiguration config)
        {
            if (models.ContainsKey(config))
            {
                return models[config];
            }
            else
            {
                var modelBuilder = DbModelHelper.CreateModelBuilder();
                DbModelHelper.ConfigModelBuilder(modelBuilder, config);
                var providerInfo = DbModelHelper.CreateProviderInfo(config);
                var model = modelBuilder.Build(providerInfo).Compile();
                models.Add(config, model);
                return model;
            }
        }
    }
}
